﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Y.Common;
namespace Demo
{
    public class TestDistributedLock
    {
        static int count = 1000;

        public static void Test1()
        {
            for (int c = 0; c < 10; c++)
            {
                count = 1000;
                List<Task> list = new List<Task>();
                for (int i = 0; i < 10; i++)
                {
                    list.Add(Task.Run(() =>
                    {
                        var num = Y.Common.RandomString.RandomDataNum(2).Parse<int>(); 
                        while (count > 0)
                        {
                            if (count < num)
                            {
                                num = count;
                            }
                            Task.Delay(2000);
                            count -= num;
                            Task.Delay(2000);
                        }
                    }));
                }
                Task.WaitAll(list.ToArray());
                list.Clear();
                Console.WriteLine(count);
            }
        }

        public static void Test2() 
        {
            for (int c = 0; c < 10; c++)
            {
                count = 1000;
                List<Task> list = new List<Task>();
                for (int i = 0; i < 10; i++)
                {
                    list.Add(Task.Run(() =>
                    {
                        var num = Y.Common.RandomString.RandomDataNum(2).Parse<int>();
                        var id = Y.Common.RandomString.CreateIDLong();
                        try
                        {
                            if (RedisService.RedisFactory.DefaultRedis.Lock("lock", id.ToString(), 3000))
                            {
                                while (count > 0)
                                {
                                    if (count < num)
                                    {
                                        num = count;
                                    }
                                    if (i % 3 == 0)
                                    {
                                        Task.Delay(4 * 1000);
                                    }
                                    else
                                    {
                                        Task.Delay(2 * 1000);
                                    }
                                    count -= num;
                                    if (i % 3 == 0)
                                    {
                                        Task.Delay(4 * 1000);
                                    }
                                    else
                                    {
                                        Task.Delay(2 * 1000);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                        }
                        finally
                        {
                            if (!RedisService.RedisFactory.DefaultRedis.Unlock("lock", id.ToString()))
                            {
                               // Console.WriteLine("超时删除失败！");
                            }
                        }
                    }));
                }
                Task.WaitAll(list.ToArray());
                list.Clear();
                Console.WriteLine(count);
            }
        }

    }

}